<?php
	class news_model extends CI_Model {

		public function __construct()
		{
			// Call the Model constructor
			parent::__construct();
		}
		public function __destruct() {
			$this->db->close();
		}
		public function news_count() {
			return $this->db->count_all("news");
		}
		
		
		public function get_list_news($limit, $offset, $sort_by, $sort_order){
			$sort_order = ($sort_order == 'Asc') ? 'Asc' : 'Desc';
			$sort_columns = array('id','title','image','type', 'time_update');
			$sort_by = (in_array($sort_by,$sort_columns)) ? $sort_by : 'time_update';
			$q = $this->db->select("*",FALSE)
				->from("news")
				->limit($limit,$offset)
				->order_by($sort_by, $sort_order);
			$ret['rows'] = $q->get()->result();

			// Count Result
			$q = $this->db->select('COUNT(*) as count',FALSE)
				->from("news");
			$tmp = $q->get()->result();
			$ret['num_rows'] = $tmp[0]->count;
			$this->db->close();
			return $ret;
		}
		
		public function get_list_news_by_time($startdate,$enddate) {
			$startdate =strtotime( date('Y/m/d 00:00:00',$startdate));
			$enddate =strtotime( date('Y/m/d 23:59:59',$enddate));			
			$this->db->select('*');
			$this->db->from('news');
			$this->db->where('time_update >', $startdate);
			$this->db->where('time_update <=', $enddate);
			$this->db->order_by("news_id", "desc"); 
			$query = $this->db->get();		
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row) {
					$data[] = $row;
				}
				return $data;
			}
			return 31;
		}
		
		public function get_list_all_news() {
			$this->db->select('*');
			$this->db->from('news');
			$this->db->order_by("news_id", "desc"); 
			$query = $this->db->get();		
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row) {
					$data[] = $row;
				}
				return $data;
			}
			return "empty";
		}
				
		public function get_news_by_id($id) {
			$q = $this->db->select('*',FALSE)
				->from("news")
				->where('news_id', $id);
			$tmp = $q->get()->result();
			if($tmp!=null)
				return $tmp[0];
			else return 31;
		}
		public function insert_news($data,$filename)
		{
			//Kiểm tra trùng tên				
			if($data['title']=="")
			{
				return 1;//không tồn tại tên
			}
			else if(strlen($data['title'])<10)
			{
				return 2;
			}
			$q = $this->db->select("*",FALSE)
				->from("news")
				->where('title', $data['title']);
			$check = $q->get()->result();
			if($check!=null)
			{
				return 13;//Đã trùng tên
			}
			else
			{
				$insert_data = array(
					'type' => $data['type'],
					'title' => $data['title'],
					'image' => $filename,
					'body' => $data['body'],
					'comment' => $data['comment'],
					'note' => $data['note'],
					'time_update'=>time());
				$this->db->insert('news', $insert_data);
				return 200;
			}
		}
		
		public function update_news($data,$filename)
		{	
			//Kiểm tra trùng tên
			$q = $this->db->select("*",FALSE)
				->from("news")
				->where('news_id !=', $data['news_id'])
				->where('title', $data['title']);
			$check = $q->get()->result();
			if($check!=null)
			{
				return 13;//Đã trùng tên
			}
			else
			{
				$this->db->where('news_id', $data['news_id']);
				$update_data = array(
					'type' => $data['type'],
					'title' => $data['title'],
					'image' => $filename,
					'body' => $data['body'],
					'comment' => $data['comment'],
					'note' => $data['note'],
					'time_update'=>time());		
				$this->db->update('news', $update_data);
				return 200;
			}		
		}
		
		
		public function delete_news($id)
		{		
			$this->db->delete('news', array('news_id' =>$id));
			return 200;
		}	
}